﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using oldislandapi.IService;

namespace oldislandapi.Controllers
{
    [Route("api/book")]
    [ApiController]
    public class BookController : ControllerBase
    {
        private IBookService _bookService;

        public BookController(IBookService bookService)
        {
            _bookService = bookService ?? throw new ArgumentNullException(nameof(bookService));
        }

        /// <summary>
        /// 获取热门书籍(概要)
        /// </summary>
        /// <returns></returns>
        [HttpGet("hot_list")]
        public async Task<ActionResult> GetHotList() => Ok(await _bookService.GetHotBookListAsync());

        /// <summary>
        /// 获取书籍短评
        /// </summary>
        /// <param name="book_id"></param>
        /// <returns></returns>
        [HttpGet("{book_id}/short_comment")]
        public async Task<ActionResult> GetShort_CommentById(int book_id)
        {
            var result = await _bookService.GetCommentByBookIdAsync(book_id);

            var dic = new Dictionary<string, object>();
            var comment = new List<object>();

            result?.ForEach(x => comment.Add(new { content = x.Content, nums = x.Nums }));
            dic.Add("book_id", book_id);
            dic.Add("comment", comment);

            return Ok(dic);
        }
    }
}
